
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
react-popper
Advanced tools
The react-popper package is a React wrapper around the Popper.js library, which is a positioning engine to ensure popovers, tooltips, and similar floating elements are positioned correctly relative to their reference elements, even in complex scenarios like inside scrolling containers or when the viewport size changes. It provides a set of React hooks and components to integrate Popper.js functionality in a React-friendly way.
Tooltip positioning
This code demonstrates how to use the `usePopper` hook to create a tooltip that is positioned relative to a button. The tooltip's position is automatically updated to ensure it remains correctly placed relative to the button, even if the layout changes.
{"import React from 'react';\nimport { usePopper } from 'react-popper';\n\nfunction Tooltip() {\n const [referenceElement, setReferenceElement] = useState(null);\n const [popperElement, setPopperElement] = useState(null);\n const { styles, attributes } = usePopper(referenceElement, popperElement);\n\n return (\n <>\n <button ref={setReferenceElement}>Hover over me</button>\n <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>\n Tooltip content\n </div>\n </>\n );\n}"}
Dropdown menu
This example shows how to create a dropdown menu using `usePopper`. The dropdown's visibility is toggled by clicking a button, and its position is dynamically adjusted to stay aligned with the button.
{"import React, { useState } from 'react';\nimport { usePopper } from 'react-popper';\n\nfunction Dropdown() {\n const [referenceElement, setReferenceElement] = useState(null);\n const [popperElement, setPopperElement] = useState(null);\n const [visible, setVisible] = useState(false);\n const { styles, attributes } = usePopper(referenceElement, popperElement);\n\n return (\n <>\n <button ref={setReferenceElement} onClick={() => setVisible(!visible)}>Toggle Dropdown</button>\n {visible && (\n <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>\n Dropdown content\n </div>\n )}\n </>\n );\n}"}
Tippy.js is a highly customizable tooltip and popover library that wraps around Popper.js, similar to react-popper but with a broader focus on tooltips and popovers. It offers a wide range of animations, themes, and interactive options out of the box, making it more feature-rich for specific tooltip and popover use cases compared to the more general positioning capabilities of react-popper.
Floating UI is the successor to Popper.js and offers direct React integration through its own React package. It provides low-level primitives for positioning floating elements, similar to react-popper, but with an updated API and additional features to handle more complex positioning scenarios. It's a more modern alternative with active development focusing on addressing limitations found in Popper.js.
React wrapper around Popper.js.
important note: popper.js is not a tooltip library, it's a positioning engine to be used to build features such as (but not restricted to) tooltips.
Via package managers:
npm install react-popper --save
# or
yarn add react-popper
Via script
tag (UMD library exposed as ReactPopper
):
<script src="https://unpkg.com/react-popper/dist/index.umd.js"></script>
Using
react-popper@0.x
? You can find its documentation clicking here
Example:
import { Manager, Reference, Popper } from 'react-popper';
const Example = () => (
<Manager>
<Reference>
{({ ref }) => (
<button type="button" ref={ref}>
Reference element
</button>
)}
</Reference>
<Popper placement="right">
{({ ref, style, placement, arrowProps }) => (
<div ref={ref} style={style} data-placement={placement}>
Popper element
<div ref={arrowProps.ref} style={arrowProps.style} />
</div>
)}
</Popper>
</Manager>
);
react-popper
makes use of a React pattern called "render prop", if you are not
familiar with it, please read more on the official React documentation.
Using React <=15 or Preact? The components created with them don't support to return fragments, this means that you will need to wrap
<Reference />
and<Popper />
into a single, common,<div />
to makereact-popper
work.
The Manager
component is a simple wrapper that needs to surround all the other react-popper
components in order
to make them communicate with each others.
The Popper
component accepts the properties children
, placement
, modifiers
, eventsEnabled
and positionFixed
.
<Popper
innerRef={(node) => this.popperNode = node}
placement="right"
modifiers={{ preventOverflow: { enabled: false } }}
eventsEnabled={true}
positionFixed={false}
>
{ props => [...] }
</Popper>
children
children: ({|
ref: (?HTMLElement) => void,
style: { [string]: string | number },
placement: ?Placement,
outOfBoundaries: ?boolean,
scheduleUpdate: () => void,
arrowProps: {
ref: (?HTMLElement) => void,
style: { [string]: string | number },
},
|}) => Node
A function (render prop) that takes as argument an object containing the following properties:
ref
: used to retrieve the React refs of the popper element.style
: contains the necessary CSS styles (React CSS properties) which are computed by Popper.js to correctly position the popper element.placement
: describes the placement of your popper after Popper.js has applied all the modifiers
that may have flipped or altered the originally provided placement
property. You can use this to alter the
style of the popper and or of the arrow according to the definitive placement. For instance, you can use this
property to orient the arrow to the right direction.outOfBoundaries
: a boolean signifying if the popper element is overflowing its boundaries.scheduleUpdate
: a function you can call to schedule a Popper.js position update. It will directly call the Popper#scheduleUpdate method.arrowProps
: an object, containing style
and ref
properties that are identical to the
ones provided as the first and second arguments of children
, but relative to the arrow element. The style
property contains left
and top
offset values, which are used to center the arrow within the popper. These values can be merged with further custom styling and positioning. See the demo for an example.innerRef
innerRef?: (?HTMLElement) => void
Function that can be used to obtain popper reference
placement
placement?: PopperJS$Placement;
One of the accepted placement values listed in the Popper.js documentation.
Your popper is going to be placed according to the value of this property.
Defaults to bottom
.
outOfBoundaries: ?boolean;
A boolean that can be used to hide the popper element in case it's overflowing from its boundaries. Read more.
eventsEnabled
eventsEnabled?: boolean;
Tells react-popper
to enable or disable the Popper.js event listeners. true
by default.
positionFixed
Set this property to true
to tell Popper.js to use the position: fixed
strategy
to position the popper element. By default it's false, meaning that it will use the
position: absolute
strategy.
modifiers
modifiers?: PopperJS$Modifiers;
An object containing custom settings for the Popper.js modifiers.
You can use this property to override their settings or to inject your custom ones.
ReactDOM.createPortal
Popper.js is smart enough to work even if the popper and reference elements aren't
in the same DOM context.
This means that you can use ReactDOM.createPortal
(or any pre React 16 alternative) to move the popper component somewhere else in the DOM.
This can be useful if you want to position a tooltip inside an overflow: hidden
container
that you want to make overflow. Please note that you can also try the positionFixed
strategy
to obtain a similar effect with less hassle.
import { Manager, Reference, Popper } from 'react-popper';
const Example = () => (
<Manager>
<Reference>
{({ ref }) => (
<button type="button" ref={ref}>
Reference
</button>
)}
</Reference>
{ReactDOM.createPortal(
<Popper>
{({ placement, ref, style }) => (
<div ref={ref} style={style} data-placement={placement}>
Popper
</div>
)}
</Popper>,
document.querySelector('#destination')
)}
</Manager>
);
HTMLElement
Whenever you need to position a popper based on some arbitrary coordinates, you can provide Popper
with a referenceElement
property that is going to be used in place of the referenceProps.getRef
React ref.
The referenceElement
property must be an object with an interface compatible with an HTMLElement
as described in the Popper.js referenceObject documentation, this implies that you may also provide a real HTMLElement if needed.
If referenceElement
is defined, it will take precedence over any referenceProps.ref
provided refs.
import { Popper } from 'react-popper';
class VirtualReference {
getBoundingClientRect() {
return {
top: 10,
left: 10,
bottom: 20,
right: 100,
width: 90,
height: 10,
};
}
get clientWidth() {
return this.getBoundingClientRect().width;
}
get clientHeight() {
return this.getBoundingClientRect().height;
}
}
// This is going to create a virtual reference element
// positioned 10px from top and left of the document
// 90px wide and 10px high
const virtualReferenceElement = new VirtualReference();
// This popper will be positioned relatively to the
// virtual reference element defined above
const Example = () => (
<Popper referenceElement={virtualReferenceElement}>
{({ ref, style, placement, arrowProps }) => (
<div ref={ref} style={style} data-placement={placement}>
Popper element
<div ref={arrowProps.ref} style={arrowProps.style} />
</div>
)}
</Popper>
);
This library is built with Flow but it supports TypeScript as well.
You can find the exported Flow types in src/index.js
, and the
TypeScript definitions in typings/react-popper.d.ts
.
git clone git@github.com:FezVrasta/react-popper.git
cd ~/react-popper
npm install
or yarn
npm run demo:dev
or yarn demo:dev
http://localhost:1234/
FAQs
Official library to use Popper on React projects
We found that react-popper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.